package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MemberService;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;
    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return menuService.findPage(queryPageBean);
    }

    @RequestMapping("/findChildrenByPid")
    public Result findChildrenByPid(Integer id){
        try {
            List<Menu> list = menuService.findChildrenByPid(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
    //新增菜单
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
    }

    //查找所有一级菜单
    @RequestMapping("/findFatherMenus")
    public Result findFatherMenus(){
        try {
           List<Menu> list = menuService.findFatherMenus();
           return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //根据id查找菜单
    @RequestMapping("/findById")
    public Result findById(Integer menuId){
        try {
           Menu menu =  menuService.findById(menuId);
           return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //编辑菜单
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }
    //删除菜单
    @RequestMapping("/delete")
    public Result deleteById(Integer id){
        try {
            menuService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
    }

    //根据登录用户名查询菜单
    @RequestMapping("/findMenuByUsername")
    public Result findMenuByUsername(){
        try {
            //通过框架提供的上下文对象获取框架的user对象
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            //通过框架的user对象获取用户名
            String username = user.getUsername();
          List<Menu> list= menuService.findMenuByUsername(username);
          return  new Result(true,MessageConstant.QUERY_MENU_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Menu> list= menuService.findAll();
            return  new Result(true,MessageConstant.QUERY_MENU_SUCCESS,list);
        }catch (Exception e){
            return  new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
}
